<?php

class Admin_AdministradorController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	$form = new SON_Forms_Busca();
        $admin = new Admin();
        $session = new Zend_Session_Namespace('admin');
         
        $conditions = array();
        if ($this->_request->isPost()) {
        	$session->post = $this->_request->getPost();
        }
         
        if ($session->post) {
        	$form->populate($session->post);
        	if ($session->post['filtro_busca']) {
        		$conditions = array('nome LIKE ?' => '%' . $session->post['filtro_busca'] . '%');
        	}
        }
        
        $this->view->messages = $this->_helper->getHelper('FlashMessenger')->getMessages();
        $this->view->form = $form;
        $this->view->administradores = $admin->search(
            array(
                'page' => $this->_getParam('page', 1),
                'conditions' => $conditions
            )
        );
    }

    public function addAction()
    {
        $form = new SON_Forms_Admin();
        
        if ($this->_request->isPost()) {
            $data = $this->_request->getPost();
            if ($form->isValid($data)) {
        
                $admin = new Admin($data);
                $admin->save();
                
                $this->_helper->FlashMessenger(
                	array('success' => 'Administrador cadastrado com sucesso')
                );
        
                $this->_redirect('admin/administrador/');
            } else {
    			$form->populate($data);
    			$this->view->error = $form->getMessages();
    		}
        }
        
        $this->view->form = $form;
    }

    public function editAction()
    {
        $admin = new Admin();
        $radmin = $admin->getAsArray((int) $this->_getParam('id', 1));
    
        $form = new SON_Forms_Admin();
        $form->setAsEditForm($radmin);
    
        if ($this->_request->isPost()) {
            $data = $this->_request->getPost();
            if ($form->isValid($data)) {
                $admin = new Admin($data);
                $admin->save();
                
                $this->_helper->FlashMessenger(
                	array('success' => 'Administrador alterado com sucesso')
                );
                
                $this->_redirect('admin/administrador/');
            } else {
                $form->populate($data);
                $this->view->error = $form->getMessages();
            }
        }
        
        $this->view->form = $form;
    }
    
    public function deleteAction()
    {
        $admin = new Admin();
        if ((int) $this->_getParam('id') > 0) {
            $admin->delete((int) $this->_getParam('id'));
        
	        $this->_helper->FlashMessenger(
	        	array('success' => 'Administrador excluído com sucesso')
	        );
        }
    
        $this->_helper->viewRenderer->setNoRender();
        $this->_redirect("admin/administrador/");
    }
    
}



